<?php
namespace Peas\Kernel\Helper;

use Peas\Kernel\PeasUtils;

/**
 * Peas Framework
 *
 * 分页html辅助类
 *
 * @author  Hongzan Zhang <zhanghongzan@163.com>
 * @version $Id$
 */
class PageHelper
{
    /**
     * 获取分页html
     *
     * @param  string $url         页面url
     * @param  int    $numRows     记录总数
     * @param  int    $currentPage 当前分页值，从1开始，默认为1
     * @param  int    $pageSize    每页数量，默认为10
     * @return string html代码
     */
    public static function getPageHtml($url, $numRows, $currentPage = 1, $pageSize = 10)
    {
        $totalPage = ceil($numRows / $pageSize);
        if ($totalPage <= 1) {
            return '';
        } elseif ($currentPage > $totalPage) {
            $currentPage = $totalPage;
        } elseif ($currentPage < 1) {
            $currentPage = 1;
        }
        $showNum = 9; // 显示数量，奇数
        $moveNum = ($showNum - 1) / 2;
        $startNum = 1;
        $totalShow = $showNum;

        if ($totalPage < $showNum) {
            $startNum = 1;
            $totalShow = $totalPage;
        } elseif($currentPage <= $moveNum) {
            $startNum = 1;
        } elseif($currentPage > ($totalPage - $moveNum)) {
            $startNum = $totalPage - $showNum + 1;
        } else {
            $startNum = $currentPage - $moveNum;
        }

        $url = $url . (strripos($url, '?') === false ? '?' : '&') . 'p=';
        $htmlArray = array();
        $htmlArray[] = '<div class="page-box"><div class="page"><a ' . ($currentPage == 1 ? 'href="javascript:;" class="disable"' : 'href="' . PeasUtils::createUrl($url . ($currentPage - 1)) . '" class="normal"') . '><span>&lt;&lt;</span> 上一页</a>';
        if ($startNum != 1) {
            $htmlArray[] = '<a href="' . PeasUtils::createUrl($url . 1) . '" class="normal">1</a><a href="javascript:;" class="not">...</a>';
        }
        $num = 0;
        for ($i = 0; $i < $totalShow; $i ++){
            $num = $startNum + $i;
            $htmlArray[] = '<a ' . ($num == $currentPage ? 'href="javascript:;" class="current"' : 'href="' . PeasUtils::createUrl($url . $num) . '" class="normal"') . '>' . $num . '</a>';
        }
        if ($startNum + $totalShow < $totalPage) {
            $htmlArray[] = '<a href="javascript:;" class="not">...</a><a href="' . PeasUtils::createUrl($url . $totalPage) . '" class="normal">' . $totalPage . '</a>';
        }
        $htmlArray[] = '<a ' . ($currentPage == $totalPage ? 'href="javascript:;" class="disable"' : 'href="' . PeasUtils::createUrl($url . ($currentPage + 1)) . '" class="normal"') . '>下一页<span> &gt;&gt;</span></a></div></div>';
        return implode('', $htmlArray);
    }


    /**
     * 获取分页html
     *
     * @param  string $url         页面url
     * @param  int    $numRows     记录总数
     * @param  int    $currentPage 当前分页值，从1开始，默认为1
     * @param  int    $pageSize    每页数量，默认为10
     * @return string html代码
     */
    public static function getAjaxPageHtml($jsFunction, $numRows, $currentPage = 1, $pageSize = 10)
    {
        $totalPage = ceil($numRows / $pageSize);
        if ($totalPage <= 1) {
            return '';
        } elseif ($currentPage > $totalPage) {
            $currentPage = $totalPage;
        } elseif ($currentPage < 1) {
            $currentPage = 1;
        }
        $showNum = 9; // 显示数量，奇数
        $moveNum = ($showNum - 1) / 2;
        $startNum = 1;
        $totalShow = $showNum;

        if ($totalPage < $showNum) {
            $startNum = 1;
            $totalShow = $totalPage;
        } elseif($currentPage <= $moveNum) {
            $startNum = 1;
        } elseif($currentPage > ($totalPage - $moveNum)) {
            $startNum = $totalPage - $showNum + 1;
        } else {
            $startNum = $currentPage - $moveNum;
        }

        $htmlArray = array();
        $htmlArray[] = '<div class="page-box"><div class="page"><a href="javascript:;" ' . ($currentPage == 1 ? 'class="disable"' : 'onclick="' . $jsFunction. '(' . ($currentPage - 1) . ');" class="normal"') . '><span>&lt;&lt;</span> 上一页</a>';
        if ($startNum != 1) {
            $htmlArray[] = '<a href="javascript:;" onclick="' . $jsFunction . '(1);" class="normal">1</a><a href="javascript:;" class="not">...</a>';
        }
        $num = 0;
        for ($i = 0; $i < $totalShow; $i ++){
            $num = $startNum + $i;
            $htmlArray[] = '<a href="javascript:;" ' . ($num == $currentPage ? 'class="current"' : 'onclick="' . $jsFunction. '(' . $num . ');" class="normal"') . '>' . $num . '</a>';
        }
        if ($startNum + $totalShow < $totalPage) {
            $htmlArray[] = '<a href="javascript:;" class="not">...</a><a href="javascript:;" onclick="' . $jsFunction . '(' . $totalPage . ');" class="normal">' . $totalPage . '</a>';
        }
        $htmlArray[] = '<a href="javascript:;" ' . ($currentPage == $totalPage ? 'class="disable"' : 'onclick="' . $jsFunction. '(' . ($currentPage + 1) . ');" class="normal"') . '>下一页<span> &gt;&gt;</span></a></div></div>';
        return implode('', $htmlArray);
    }
}
